class ProReviewsController < ApplicationController
  # GET /pro_reviews
  # GET /pro_reviews.xml
  before_filter :login_required ,:only=>[:create]
  before_filter :admin?,:only=>[:edit,:destroy,:update,:list]
 
  def index
    if request.xhr?
      @pro_reviews = ProReview.find(:all,:conditions=>params[:product_id] ? ["product_id=?",params[:product_id]]:nil,:order=>"created_at asc",:joins=>" as a inner join users b on a.user_id=b.id",:select=>"a.*,b.login" ,:limit=>10)
    else
      @product=Product.find(params[:product_id]) if params[:product_id]
      @pro_reviews = ProReview.paginate(:all,:order=>"created_at asc",:page=>params[:page],:conditions=>params[:product_id] ? ["product_id=?",params[:product_id]]:nil,:joins=>" as a inner join users b on a.user_id=b.id",:select=>"a.*,b.login" )
    end
    @page_title="产品购前咨询"
    @page_nav=">&nbsp;产品购前咨询"
    respond_to do |format|
      format.html # index.html.erb
      format.js { @pro_reviews.blank? ? render(:text=>"",:layout=>"none") : render(:partial=>"item",:collection=>@pro_reviews,:layout=>"none")  }
      format.xml  { render :xml => @pro_reviews }
    end
  end
  def list
    @pro_reviews = ProReview.paginate(:all,:page=>params[:page],:order=>"created_at desc")
    respond_to do |format|
      format.html{ render :layout=>"admin"}
      format.js {render :partial=>"item",:collection=>@pro_reviews}
      format.xml  { render :xml => @pro_reviews }
    end
  end


  # GET /pro_reviews/1
  # GET /pro_reviews/1.xml
  def show
    @pro_review = ProReview.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @pro_review }
    end
  end

  # GET /pro_reviews/new
  # GET /pro_reviews/new.xml
  def new
    @pro_review = ProReview.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @pro_review }
    end
  end

  # GET /pro_reviews/1/edit
  def edit
    @pro_review = ProReview.find(params[:id])
    render :layout=>"admin"
  end

  # POST /pro_reviews
  # POST /pro_reviews.xml
  def create
    @pro_review = ProReview.new(params[:pro_review])
    @pro_review.product_id=params[:product_id]
    @pro_review.user_id=session[:user_id]
    respond_to do |format|
      if @pro_review.save
        flash[:notice] = 'ProReview was successfully created.'
        format.html { redirect_to(product_pro_reviews_path(params[:product_id])) }
        format.js {}
        format.xml  { render :xml => @pro_review, :status => :created, :location => @pro_review }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @pro_review.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /pro_reviews/1
  # PUT /pro_reviews/1.xml
  def update
    @pro_review = ProReview.find(params[:id])

    respond_to do |format|
      if @pro_review.update_attributes(params[:pro_review])
        flash[:notice] = 'ProReview was successfully updated.'
        format.html { redirect_to(list_product_pro_reviews_path(0)) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit",:layout=>"admin" }
        format.xml  { render :xml => @pro_review.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /pro_reviews/1
  # DELETE /pro_reviews/1.xml
  def destroy
    @pro_review = ProReview.find(params[:id])
    @pro_review.destroy

    respond_to do |format|
      format.html { redirect_to(pro_reviews_url) }
      format.xml  { head :ok }
    end
  end
end
